package com.exchange.controller;

import com.exchange.service.ResourceLimitService;
import com.exchange.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/resources")
@CrossOrigin(origins = "*")
@PreAuthorize("hasRole('MERCHANT') or hasRole('ADMIN')")
public class ResourceController {
    
    @Autowired
    private ResourceLimitService resourceLimitService;
    
    @GetMapping("/usage")
    public ResponseEntity<ResourceLimitService.ResourceUsage> getResourceUsage() {
        // Get the current user's ID from the security context
        Long currentUserId = SecurityUtils.getCurrentUserId();
        
        // In a real application, we would get the merchant ID from the user profile
        // For now, using a placeholder based on user ID
        Long currentMerchantId = currentUserId; // This would be looked up in a real app
        
        ResourceLimitService.ResourceUsage usage = resourceLimitService.getUsageForMerchant(currentMerchantId);
        return ResponseEntity.ok(usage);
    }
    
    @GetMapping("/limits")
    public ResponseEntity<?> getResourceLimits() {
        return ResponseEntity.ok("Resource limits will be returned here");
    }
}